
package com.ami.kvm.jviewer.videorecord;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.communication.Connection;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

public class URLProcessor extends URLHandler {
	private byte[] data;
	private static final int FILE_LOCKED = -1;
	public static final int FILE_NOT_FOUND = -2;
	public static final int INVALID_SESSION_TOKEN = 2;
	public static final int HTTP_CONNECTION_SUCCESS = 0;
	private static final int HTTP_REQUEST_SUCCESS = 200;
	public static final int HTTP_REQUEST_FAILURE = -1;
	public static final int INVALID_CREDENTIALS = -6;
	public static final int INVALID_SERVER_CERT = -7;
	private static final int MAX_BUFFER_SIZE = 1048576;
	private static final String DEF_DELIMITER = " ";
	private static final int HTTP_FILE_NOT_FOUND = 404;
	private static final String UNAUTHORIZED = "401 Unauthorized";
	private static final String NOT_FOUND = "404 Not Found";
	private static final String DYNAMIC_DATA_END = "//Dynamic data end";
	private static final String ZERO = "0";
	private static final int SUCCESS = 0;
	private static final int FAILURE = -1;
	private String hostIP = null;
	private String uriString = null;
	private static Socket socket = null;

	private final String USER_AGENT_NAME = "JViewer";
	private final String USER_AGENT_TAG = "User-Agent";

	private final String NEWLINE = "\r\n";
	private static String csrfToken = null;

	public URLProcessor(String paramString, int paramInt) {
		this.sesCookie = paramString;
		this.secureConnect = paramInt;
	}

	public URLProcessor() {
	}

	public int processRequest(String paramString) {
		int i = 0;
		int j = -1;

		if (JViewer.isRestService()) {
			return restProcessRequest(paramString);
		}

		if (socket == null) {

			try {

				int k = JViewer.isWebSecure() == true ? 1 : 0;
				socket = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(JViewer.getIp()),
						JViewer.getWebPort(), k);
			} catch (UnknownHostException localUnknownHostException) {
				Debug.out.println(localUnknownHostException);
			}

			if (socket == null) {
				return -7;
			}
		}

		this.uriString = paramString;

		if (writeRequestToSocket(socket, null) != 0) {
			return -1;
		}

		if (paramString.contains(".asp")) {

			j = getServerResponseData();
			if (j != 0) {
				return j;
			}

		} else {
			return getDataFromSocket();
		}

		i = Integer.parseInt(getValueOf(new String(this.data), "HAPI_STATUS:", ' ').trim());

		if (i < 0) {
			if ((JViewer.isdownloadapp()) || (JViewer.isplayerapp())) {
				JViewerApp.getInstance().getVideorecordapp().disposeInformationDialog();
				if (i == -1) {
					JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(),
							LocaleStrings.getString("Z_2_URLP"), LocaleStrings.getString("A_5_GLOBAL"), 0);

					return -1;
				}
				if (i == -2) {
					JOptionPane.showMessageDialog(JViewerApp.getInstance().getMainWindow(),
							LocaleStrings.getString("Z_3_URLP"), LocaleStrings.getString("A_5_GLOBAL"), 0);

					return -2;
				}
			}

			if (i == -6) {
				return -6;
			}
		}

		return i;
	}

	public String getValue(String paramString, char paramChar) {
		String str1 = null;
		String str2 = new String(this.data);
		int i = str2.indexOf(paramString);
		int j = str2.indexOf(paramChar, i + paramString.length());
		if (j < 0) {
			j = str2.indexOf(" ", i + paramString.length());
		}

		if ((i > 0) && (j > 0)) {
			str1 = str2.substring(i + paramString.length(), j).trim();
		}

		return str1;
	}

	public String getValueOf(String paramString1, String paramString2, char paramChar) {
		String str = null;
		int i = paramString1.indexOf(paramString2) + paramString2.length();
		int j = paramString1.indexOf(paramChar, i);
		str = paramString1.substring(i, j).trim();
		return str;
	}

	public byte[] getData() {
		return this.data;
	}

	public void setData(byte[] paramArrayOfByte) {
		this.data = paramArrayOfByte;
	}

	public int download(String paramString) throws IOException {
		int i = 0;
		long l = 0L;

		this.uriString = paramString;
		int j = 0;

		if (socket == null) {
			return -1;
		}

		if (writeRequestToSocket(socket, null) != 0) {
			return -1;
		}

		File localFile = new File(JViewerApp.getInstance().getVideorecordapp().Videotmpstorepath[0]);
		FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
		BufferedInputStream localBufferedInputStream = new BufferedInputStream(socket.getInputStream());
		try {
			byte[] arrayOfByte = new byte[1048576];

			if (getHTTPResponseHeader(localBufferedInputStream) != 0) {
				return -1;
			}
			if (this.data.toString().contains("404 Not Found")) {
				return -2;
			}
			int k = Integer.parseInt(getValueOf(new String(this.data), "Content-Length:", '\r').trim());

			while ((i = localBufferedInputStream.read(arrayOfByte)) != -1) {
				localFileOutputStream.write(arrayOfByte, 0, i);

				l += i;

				if (l >= k) {
					Debug.out.println("Read till content length !");
				}
			}

			return 0;
		} catch (Exception localException) {
			Debug.out.println("Exception : while reading bytes from input stream " + localException);
			j = 1;
		} finally {
			try {
				localFileOutputStream.close();

				if (j != 0) {
					localBufferedInputStream.close();
					return -1;
				}
			} catch (IOException localIOException5) {
				Debug.out.println(localIOException5);
				return -1;
			}
		}
		return -1;
	}

	public int writeRequestToSocket(Socket paramSocket, String paramString)
   {
     if (JViewer.isRestService()) {
       return restWriteRequestToSocket(paramSocket, paramString);
     }
     
     int i = 0;
     PrintWriter localPrintWriter = null;
     
     try
     {
       localPrintWriter = new PrintWriter(new OutputStreamWriter(paramSocket.getOutputStream()));
       localPrintWriter.print("GET " + this.uriString + " HTTP/1.1" + "\r\n");
       localPrintWriter.print("Host: " + this.hostIP + "\r\n");
       localPrintWriter.print("User-Agent: JViewer\r\n");
       if (this.sesCookie != null) {
         localPrintWriter.print("Cookie: SessionCookie=" + this.sesCookie + "\r\n");
       }
       if ((paramString != null) && (paramString.length() > 0))
         localPrintWriter.print(paramString + "\r\n");
       localPrintWriter.print("\r\n");
       localPrintWriter.flush();
     } catch (IOException localIOException) {
       Debug.out.println(localIOException);
       i = -1;
     }
     return i;
   }

	public int getServerResponseData() {
		int i = 0;
		BufferedReader localBufferedReader = null;
		String str1 = "";
		try {
			localBufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			for (String str2 = ""; (str2 = localBufferedReader.readLine()) != null;) {
				if ((str2.startsWith("//Dynamic data end")) || (str2.equals("0"))) {
					break;
				}
				if (str2.contains("404 Not Found")) {

					socket.close();
					setSocket(null);
					i = -2;
					break;
				}
				if (str2.contains("401 Unauthorized")) {

					socket.close();
					setSocket(null);
					i = -6;
					break;
				}
				str1 = str1 + str2 + "\n";
			}
		} catch (Exception localException) {
			String str2;
			i = -1;
			setSocket(null);
			Debug.out.println(localException);
		}
		this.data = str1.getBytes();
		return i;
	}

	public int getHTTPResponseHeader(InputStream paramInputStream) {
		int i = 0;
		int j = 0;
		String str = null;
		try {
			try {
				while ((i = paramInputStream.read()) != -1) {
					str = str + (char) i;
					if (str.contains("\r\n\r\n")) {
						Debug.out.println("Read till header end");
					}
				}
			} catch (IOException localIOException) {
				Debug.out.println(localIOException);
				j = -1;
			}
		} catch (Exception localException) {
			Debug.out.println(localException);
			j = -1;
		}
		if (j == 0)
			this.data = str.getBytes();
		return j;
	}

	public static Socket getSocket() {
		return socket;
	}

	public static void setSocket(Socket paramSocket) {
		socket = paramSocket;
	}

	private int getDataFromSocket() {
		BufferedInputStream localBufferedInputStream = null;
		String str = null;
		try {
			localBufferedInputStream = new BufferedInputStream(socket.getInputStream());
		} catch (IOException localIOException1) {
			Debug.out.println(localIOException1);
			return -1;
		}
		ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
		int i = 0;
		int j = 0;

		if (getHTTPResponseHeader(localBufferedInputStream) != 0) {
			return -1;
		}
		if (this.data != null) {
			str = new String(this.data);
		} else {
			return -1;
		}

		if ((str.contains("404")) || (str.contains("404&nbsp;Not Found"))) {
			return -2;
		}
		int k = Integer.parseInt(getValueOf(str, "Content-Length:", '\r').trim());
		try {
			while ((i = localBufferedInputStream.read()) != -1) {
				j++;
				if (j >= k) {
					break;
				}

				localByteArrayOutputStream.write(i);
			}
		} catch (IOException localIOException2) {
			Debug.out.println(localIOException2);
			return -1;
		}
		this.data = localByteArrayOutputStream.toByteArray();
		try {
			localByteArrayOutputStream.flush();
			localByteArrayOutputStream.close();
			localBufferedInputStream.close();
			socket.close();
			setSocket(null);
		} catch (IOException localIOException3) {
			Debug.out.println(localIOException3);
			return -1;
		}
		return 0;
	}

	public String getUriString() {
		return this.uriString;
	}

	public void setUriString(String paramString) {
		this.uriString = paramString;
	}

	public String getHostIP() {
		return this.hostIP;
	}

	public void setHostIP(String paramString) {
		this.hostIP = paramString;
	}

	public int restProcessRequest(String paramString) {
		Debug.out.println("urlPath: " + paramString);
		int i = -1;

		if (socket == null) {

			try {

				int j = JViewer.isWebSecure() == true ? 1 : 0;
				socket = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(JViewer.getIp()),
						JViewer.getWebPort(), j);
			} catch (UnknownHostException localUnknownHostException) {
				Debug.out.println(localUnknownHostException);
			}

			if (socket == null) {
				return -7;
			}
		}

		this.uriString = paramString;

		if (restWriteRequestToSocket(socket, null) != 0) {
			return -1;
		}
		if (paramString.contains("/api/logs/help-data")) {
			i = getDataFromSocket();
		} else
			i = getServerResponseData();
		return i;
	}

	public int restWriteRequestToSocket(Socket paramSocket, String paramString) {
     int i = 0;
     PrintWriter localPrintWriter = null;
     
     try
     {
       localPrintWriter = new PrintWriter(new OutputStreamWriter(paramSocket.getOutputStream()));
       if ((csrfToken == null) && (this.sesCookie == null)) {
         localPrintWriter.print("POST " + this.uriString + " HTTP/1.1" + "\r\n");
       }
       else if (this.uriString.contains("/api/logs/video-log")) {
         localPrintWriter.print("PUT " + this.uriString + " HTTP/1.1" + "\r\n");
       }
       else if ((this.uriString.contains("/kvm/token")) || (this.uriString.contains("/settings/media/instance")) || (this.uriString.contains("/api/settings/media/adviser")) || (this.uriString.contains("/api/logs/video")) || (this.uriString.contains("/api/logs/help-data"))) {
         localPrintWriter.print("GET " + this.uriString + " HTTP/1.1" + "\r\n");
       }
       else {
         localPrintWriter.print("DELETE " + this.uriString + " HTTP/1.1" + "\r\n");
       }
       
       localPrintWriter.print("Host: " + this.hostIP + "\r\n");
       
       localPrintWriter.print("Connection: Keep-Alive\r\n");
       
       if (this.uriString.contains("/api/session")) {
         localPrintWriter.print("Content-Length: 0\r\n");
       }
       if (csrfToken != null) {
         localPrintWriter.print("X-CSRFTOKEN: " + csrfToken + "\r\n");
       }
       
       if (this.sesCookie != null) {
         localPrintWriter.print("Cookie: " + this.sesCookie + "\r\n");
       }
       if ((paramString != null) && (paramString.length() > 0)) {
         localPrintWriter.print(paramString + "\r\n");
       }
       localPrintWriter.print("\r\n");
       localPrintWriter.flush();
     } catch (IOException localIOException) {
       Debug.out.println(localIOException);
       i = -1;
     }
     return i;
   }

	public static String getCsrfToken() {
		return csrfToken;
	}

	public static void setCsrfToken(String paramString) {
		csrfToken = paramString;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\videorecord\
 * URLProcessor.class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */